package com.base.common.webservice.util;

import com.base.common.json.JsonUtil;
import com.base.common.webservice.HZSYS86Service.CstService;
import com.base.common.webservice.HZSYS86Service.CstServiceSoap;
import com.base.common.webservice.ServiceConfigConstants;
import com.base.common.webservice.entity.HZService.HZServiceArgs;
import com.base.common.webservice.entity.HZService.UserCstGetUsersByMobilePhoneBean;
import com.base.common.webservice.entity.HZService.UserRevGetFeeListbyLocklogoBean;
import com.base.common.webservice.entity.HZService.UserRevTradingByCstIDBean;
import com.base.common.webservice.factory.HZServiceFactory;
import com.base.common.webservice.factory.WebServiceFactory;
import org.apache.commons.collections.map.HashedMap;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

/**
 * Created by Kevin on 2017-5-17.
 */
public class HZServiceUtil {

    private static Map<String, Object> getHZService(HZServiceArgs hzServiceArgs) {
        WebServiceFactory webServiceFactory = HZServiceFactory.getInstance();
        HZServiceFactory hzServiceFactory = (HZServiceFactory) webServiceFactory;

        // 获取service
        CstService cstService = hzServiceFactory.getCstService();
        CstServiceSoap cstServiceSoap = cstService.getCstServiceSoap();
        List<Object> objects  = (List<Object>) (JsonUtil.string2Obejct(cstServiceSoap.getETSService("SSSySWIN*(SK_WH()",hzServiceArgs.getP0(), hzServiceArgs.getP1(),
                hzServiceArgs.getP2(), hzServiceArgs.getP3(),
                hzServiceArgs.getP4(), hzServiceArgs.getP5(),
        hzServiceArgs.getP6(), hzServiceArgs.getP7()), Map.class).get(hzServiceArgs.getP0()));
        Map<String, Object> map = (Map<String, Object>) objects.get(0);
        /*List<T> ts = new ArrayList<T>();
        for (Map<String, Object> object : (List<Map<String, Object>>) map
                .get("Syswin")) {
//            System.out.println(StringEscapeUtils.unescapeJava(object.get("CstName").toString()));
//            System.out.println(JsonUtil.dto2String(object));
            T t = EasyUtils.mapToObject(clazz, object);
            ts.add(t);
        }*/
        return map;
    }

    public static List<UserCstGetUsersByMobilePhoneBean> getUserCstGetUsersByMobilePhone(String phone) {
        HZServiceArgs hzServiceArgs = new HZServiceArgs();
        hzServiceArgs.setP0(ServiceConfigConstants.HZ_USER_CST_GETUSERBYMOBILEPHONE);
        hzServiceArgs.setP1(phone);
        Map<String, Object> objectMap = getHZService(hzServiceArgs);
        List<UserCstGetUsersByMobilePhoneBean> ts = new ArrayList<UserCstGetUsersByMobilePhoneBean>();
        for (Map<String, Object> object : (List<Map<String, Object>>) objectMap
                .get("Table")) {
            UserCstGetUsersByMobilePhoneBean t = EasyUtils.mapToObject(UserCstGetUsersByMobilePhoneBean.class, object);
            ts.add(t);
        }
        return ts;
    }

    public static List<UserRevGetFeeListbyLocklogoBean> getUserRevGetFeeListbyLocklogoBean(String cstID,String OrgID, String expensesCate) {
        HZServiceArgs hzServiceArgs = new HZServiceArgs();
        hzServiceArgs.setP0(ServiceConfigConstants.HZ_USERREV_GETFEELISTBYLOCKLOGO);
        hzServiceArgs.setP1(cstID);
        hzServiceArgs.setP4(OrgID);
        hzServiceArgs.setP5(expensesCate);
        Map<String, Object> objectMap = getHZService(hzServiceArgs);
        List<UserRevGetFeeListbyLocklogoBean> ts = new ArrayList<UserRevGetFeeListbyLocklogoBean>();
        for (Map<String, Object> object : (List<Map<String, Object>>) objectMap
                .get("Syswin")) {
            UserRevGetFeeListbyLocklogoBean t = EasyUtils.mapToObject(UserRevGetFeeListbyLocklogoBean.class, object);
            ts.add(t);
        }
        return ts;
    }

    public static List<UserRevTradingByCstIDBean> getUserRevTradingByCstID (String cstID) {
        HZServiceArgs hzServiceArgs = new HZServiceArgs();
        hzServiceArgs.setP0(ServiceConfigConstants.HZ_USERREV_TRADINGBYCSTID);
        hzServiceArgs.setP1(cstID);
        Map<String, Object> objectMap = getHZService(hzServiceArgs);
        List<UserRevTradingByCstIDBean> ts = new ArrayList<UserRevTradingByCstIDBean>();
        for (Map<String, Object> object : (List<Map<String, Object>>) objectMap
                .get("Syswin")) {
            UserRevTradingByCstIDBean t = EasyUtils.mapToObject(UserRevTradingByCstIDBean.class, object);
            ts.add(t);
        }
        return  ts;
    }

    public static int addUserRevPayFeeByBank(String RevID, double RevMoney, String TradingID, String Trading, String Filldate, String OrgID) {
        try {
            Map m = new LinkedHashMap();
            m.put("RevID", RevID);
            m.put("RevMoney", RevMoney);
            m.put("LFMoney", 0);
            m.put("TradingID", TradingID);
            m.put("Trading", Trading);
            m.put("Filldate", Filldate);
            m.put("RBank", "");
            m.put("RAccount", "");
            m.put("OrgID", OrgID);
            List list = new ArrayList<>();
            list.add(m);
            Map p1 = new HashedMap();
            p1.put("Syswin", list);
            HZServiceArgs hzServiceArgs = new HZServiceArgs();
            hzServiceArgs.setP0(ServiceConfigConstants.HZ_USERREV_PAYFEEBYBANK);
            hzServiceArgs.setP1(JsonUtil.dto2String(p1));
            Map<String, Object> hzServiceResult = getHZService(hzServiceArgs);
            System.out.println(JsonUtil.dto2String(hzServiceResult));
            int result = Integer.valueOf(hzServiceResult.get("State").toString());
            return result;
        } catch (Exception e){
            return 0;
        }
    }
    public static void main(String[] args){
//        System.out.println(JsonUtil.dto2String(getUserCstGetUsersByMobilePhone("13267007212")));
//        System.out.println(JsonUtil.dto2String(getUserRevGetFeeListbyLocklogoBean("17051016051400030064", "10000",  "0")));
//        System.out.println(JsonUtil.dto2String(addUserRevPayFeeByBank("20170510161049500002", 0.01, "38a77aa456fc813af07bb428f2363c8d", "支付宝", new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "10000")));
        System.out.println(JsonUtil.dto2String(getUserRevTradingByCstID("17051016051400030064")));
    }
}
